package cn.yunhe.service.impl;

import cn.yunhe.dao.PayBackDao;
import cn.yunhe.entity.Case;
import cn.yunhe.entity.PayBack;
import cn.yunhe.service.PayBackService;
import cn.yunhe.util.DateUtil;
import cn.yunhe.util.UUIDGenerator;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("payBackService")
public class PayBackServiceImpl implements PayBackService {
    @Resource
    private PayBackDao payBackDao;

    @Override
    public boolean addPayRecord(PayBack payBack) {
        payBack.setPayUuid(UUIDGenerator.getUUID());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String date=sdf.format(new Date())+new Date().getTime();
        payBack.setPaySerial(date);
        payBack.setPayDate(DateUtil.date2StringDateTime(new Date()));
        payBack.setPayStatus(1);
        boolean result=payBackDao.addPayRecord(payBack);
        if(result){
            Case cases=new Case();
            cases.setCaseId(payBack.getCases().getCaseId());
            result= payBackDao.updateCaseState(cases);
            return result;
        }else{
            return false;
        }
    }

    @Override
    public boolean deletePayRecordById(PayBack payBack) {
        return payBackDao.deletePayRecordById(payBack);
    }

    @Override
    public Boolean batchDeletePayRecord(List<String> uuidList) {
        return payBackDao.batchDeletePayRecord(uuidList);
    }

    @Override
    public boolean updatePayBackStatus(PayBack payBack) {
        return payBackDao.updatePayBackStatus(payBack);
    }

    @Override
    public List<PayBack> queryAllPayList() {
        return payBackDao.queryAllPayList();
    }

    @Override
    public PageInfo<PayBack> queryPayBackListByLike(Map<String, Object> maps) {
        int pageNum = Integer.parseInt(String.valueOf(maps.get("pageNum")));
        int pageSize = Integer.parseInt(String.valueOf(maps.get("pageSize")));
        PageHelper.startPage(pageNum, pageSize);
        List<PayBack> payBacks = payBackDao.queryPayBackListByLike(maps);
        PageInfo<PayBack> pageInfo = new PageInfo<>(payBacks);
        return pageInfo;
    }

    @Override
    public PageInfo<PayBack> queryPayRecordByStatus(Map<String, Object> map) {
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        PageHelper.startPage(pageNum, pageSize);
        List<PayBack> payList = payBackDao.queryPayRecordByStatus(map);
        PageInfo<PayBack> pageInfo = new PageInfo<>(payList);
        return pageInfo;
    }

    @Override
    public PageInfo<PayBack> queryUnPayRecord(Map<String, Object> map) {
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        PageHelper.startPage(pageNum, pageSize);
        List<PayBack> pays = payBackDao.queryUnPayRecord(map);
        PageInfo<PayBack> pageInfo = new PageInfo<>(pays);
        return pageInfo;
    }

    @Override
    public PageInfo<PayBack> queryIsPayRecord(Map<String, Object> map) {
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        PageHelper.startPage(pageNum, pageSize);
        List<PayBack> payBacks = payBackDao.queryIsPayRecord(map);
        PageInfo<PayBack> pageInfo = new PageInfo<>(payBacks);
        return pageInfo;
    }


    @Override
    public Integer countPayRecordNum(Map<String, Object> maps) {
        return payBackDao.countPayRecordNum(maps);
    }

    @Override
    public Case queryCaseByCaseId(String caseId) {
        return payBackDao.queryCaseByCaseId(caseId);
    }

    @Override
    public PageInfo<Case> queryCaseListUnPay(Map<String, Object> maps) {
        int pageNum = Integer.parseInt(String.valueOf(maps.get("pageNum")));
        int pageSize = Integer.parseInt(String.valueOf(maps.get("pageSize")));
        PageHelper.startPage(pageNum, pageSize);
        List<Case> caseList = payBackDao.queryCaseListUnPay(maps);
        PageInfo<Case> pageInfo = new PageInfo<>(caseList);
        return pageInfo;
    }

    @Override
    public Boolean updateCaseState(Case cases) {
        return payBackDao.updateCaseState(cases);
    }

    public PayBackDao getPayBackDao() {
        return payBackDao;
    }

    public void setPayBackDao(PayBackDao payBackDao) {
        this.payBackDao = payBackDao;
    }
}
